package com.yuven.appframework.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.PagerAdapter

/**
 *
 * 基础FragmentPagerAdapter
 * @author :        Yuven
 * @date :          2021/9/6 9:28 上午
 * @desc :
 */
class BaseViewPagerAdapter : FragmentStatePagerAdapter {
    private var fragments: MutableList<Fragment>? = null
    private var tabs: MutableList<String>? = null

    constructor(fm: FragmentManager) : super(fm) {
    }

    constructor(fm: FragmentManager, fragments: MutableList<Fragment>) : super(fm) {
        this.fragments = fragments
    }

    constructor(
        fm: FragmentManager,
        fragments: MutableList<Fragment>,
        tabs: MutableList<String>
    ) : super(fm) {
        this.fragments = fragments
        this.tabs = tabs
    }

    fun setFragments(fragments: MutableList<Fragment>?, tabs: MutableList<String>?) {
        this.fragments = fragments
        this.tabs = tabs
        notifyDataSetChanged()
    }

    fun setFragment(fragments: List<Fragment>?, tabs: List<String>?) {
        if (this.fragments != null) {
            this.fragments!!.clear()
            this.fragments!!.addAll(fragments!!)
        }
        if (this.tabs != null) {
            this.tabs!!.clear()
            this.tabs!!.addAll(tabs!!)
        }
        notifyDataSetChanged()
    }

    override fun getItem(position: Int): Fragment {
        return fragments!![position]
    }

    override fun getCount(): Int {
        return if (fragments == null) 0 else fragments!!.size
    }

    override fun getItemPosition(`object`: Any): Int {
        return PagerAdapter.POSITION_NONE
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return if (tabs == null) "" else tabs!![position]
    }

    fun getFragments(): List<Fragment>? {
        return fragments
    }
}